package com.syt.cellphone.information.base

import android.os.Build
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.WindowManager
import androidx.annotation.CallSuper
import androidx.appcompat.app.AppCompatActivity
import com.syt.cellphone.information.R
import com.syt.cellphone.information.constant.Theme
import com.syt.cellphone.information.lib.theme.ATH
import com.syt.cellphone.information.lib.theme.ColorUtils
import com.syt.cellphone.information.lib.theme.primaryColor
import com.syt.cellphone.information.utils.applyOpenTint
import com.syt.cellphone.information.utils.applyTint
import com.syt.cellphone.information.utils.disableAutoFill
import com.syt.cellphone.information.utils.hideSoftInput
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel

/**
 *  @author: syt
 *  创建时间: 2020/8/14 11:36
 *  @功能   基础活动
 *  @version v1.0
 */
abstract class BaseActivity (
    // 布局id
    private val layoutID: Int,
    // 是否全屏
    private val fullScreen: Boolean = true,
    // 界面主题
    private val theme: Theme = Theme.Auto
) : AppCompatActivity(), CoroutineScope by MainScope() {

    override fun onCreate(savedInstanceState: Bundle?) {
        window.decorView.disableAutoFill()
        initTheme()
        setupSystemBar()
        super.onCreate(savedInstanceState)
        setContentView(layoutID)
        onActivityCreated(savedInstanceState)
        observeLiveBus()
    }

    // 子类继承后,重写这个方法,必须调用父类的super方法
    @CallSuper
    override fun onDestroy() {
        super.onDestroy()
        cancel()
    }

    // 设置主题
    private fun initTheme() {
        ATH.applyBackgroundTint(window.decorView)
        when (theme) {
            Theme.Dart -> setTheme(R.style.AppTheme_Dark)
            Theme.Light -> setTheme(R.style.AppTheme_Light)
            else -> if (ColorUtils.isColorLight(primaryColor)) {
                setTheme(R.style.AppTheme_Light)
            } else {
                setTheme(R.style.AppTheme_Dark)
            }
        }
    }

    // 设置标题样式
    private fun setupSystemBar() {
        if (fullScreen) {
            window.clearFlags(
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    or WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
            )
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            }
            window.decorView.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        }
        ATH.setStatusBarColorAuto(this, fullScreen)
        if (theme == Theme.Dart) {
            ATH.setLightStatusBar(this, false)
        } else if (theme == Theme.Light) {
            ATH.setLightNavigationBar(this, true)
        }
        ATH.setNavigationBarColorAuto(this)
    }

    abstract fun onActivityCreated(savedInstanceState: Bundle?)

    open fun observeLiveBus() {
    }

    override fun finish() {
        currentFocus?.hideSoftInput()
        super.finish()
    }

    // 初始化菜单
    final override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        return menu?.let {
            val bool = onCompatCreateOptionsMenu(menu)
            it.applyTint(this, theme)
            bool
        } ?: super.onCreateOptionsMenu(menu)
    }

    open fun onCompatCreateOptionsMenu(menu: Menu): Boolean {
        return super.onCreateOptionsMenu(menu)
    }

    // 菜单栏
    final override fun onMenuOpened(featureId: Int, menu: Menu): Boolean {
        menu.applyOpenTint(this)
        return super.onMenuOpened(featureId, menu)
    }

    final override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return if (item.itemId == android.R.id.home) {
            supportFinishAfterTransition()
            true
        } else {
            super.onOptionsItemSelected(item)
        }
    }

    open fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
        return super.onOptionsItemSelected(item)
    }

    open fun upNavigationBarColor() {
        ATH.setNavigationBarColorAuto(this)
    }
}